package org.utils.print;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.attribute.DocAttribute;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;

/** Represents a doc to print
 * @author Manuel EVENO
 */

public class PrintableDoc implements Doc
{
	private DocFlavor docFlavor;
	private InputStream inStream;
	private DocAttributeSet aset = null;
	/** Constructs a printable doc with the specified filename
         * @param filename The filename of the doc (with path)
         * @throws IOException Occurs with an IO problem
         */
	public PrintableDoc(String filename) throws IOException
	{
		this(new FileInputStream(filename));
	}

        /** Constructs a printable doc with the specified filename
         * @param file The File Object representing the document to print
         * @throws IOException Occurs with an IO problem
         */        
	public PrintableDoc(File file) throws IOException
	{
		this(new FileInputStream(file));
	}

        /** Constructs a printable doc with the specified filename
         * @param stream The Stream Object representing the document to print
         * @throws IOException Occurs with an IO problem
         */        
	public PrintableDoc(InputStream stream) throws IOException
	{
		inStream = stream;
		docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
		aset = new HashDocAttributeSet();
	}
	/**
	 * Returns the docFlavor.
	 * @return DocFlavor
	 */
	public DocFlavor getDocFlavor()
	{
		return docFlavor;
	}
	/**
	 * Sets the docFlavor.
	 * @param pDocFlavor The docFlavor to set
	 */
	public void setDocFlavor(DocFlavor pDocFlavor)
	{
		this.docFlavor = pDocFlavor;
	}
        /** Adds an Attribute to the document attribute set
         * @param attr The Attribute of the document
         */        
	public void addAttribute(DocAttribute attr)
	{
		aset.add(attr);
	}
        /** Returns the document attribute set
         * @return The attribute of the document
         */        
	public DocAttributeSet getAttributes()
	{
		return aset;
	}
        /** Returns the data to print. Make a call to getStreamForBytes()
         * @throws IOException Occurs with an IO problem
         * @return The data to print
         * @see #getStreamForBytes()
         */        
	public Object getPrintData() throws IOException
	{
		return getStreamForBytes();
	}
        /** Does nothing
         * @throws IOException Never occurs, not used
         * @return null
         */        
	public java.io.Reader getReaderForText() throws IOException
	{
		return null;
	}
        /** Return the file data stream
         * @throws IOException Occurs with an IO problem
         * @return the file data stream
         */        
	public InputStream getStreamForBytes() throws IOException
	{
		return inStream;
	}
}
